package com.hospital.drug.drugyan.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.hospital.drug.drugyan.entity.Drug_record;
import com.hospital.drug.drugyan.entity.Y_drugSelect;
import com.hospital.drug.drugyan.service.DrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @description：
 * @author：zou
 * @date：2021/9/22 19:51
 * @versions：v1.0
 */
@Controller
@RequestMapping("/drugY")
public class DrugController {

    @Autowired
    private DrugService drugService;

    @RequestMapping("/drugPage")
    public ModelAndView drugPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "") String id, @RequestParam(defaultValue = "") String all, @RequestParam(defaultValue = "") String all2) {
        ModelAndView mv = new ModelAndView();
        PageInfo<Y_drugSelect> page = drugService.pageListDrug(pageNum, id, all, all2);
        mv.addObject("id", id);
        mv.addObject("page", page);
        mv.setViewName("drug/drugyan/drugSelect");
        return mv;
    }

    @RequestMapping("/selDrug")
    @ResponseBody
    public String selDrug(String id, String date) {
        Integer userId = Integer.parseInt(id.trim());
        List<Drug_record> drugs = drugService.selectAllDrug(userId, date);
        return JSON.toJSONString(drugs);
    }

    @RequestMapping("/updStatus")
    @ResponseBody
    public String updStatus(String id, String date, String shu1, String shu2) {
        String[] split1 = shu1.split("-");
        String[] split2 = shu2.split("-");
        int i = drugService.selStatus(Integer.parseInt(id), date);
        String str = "";
        if (i == 0) {
            str = "该患者未缴费!";
        } else if (i == 1) {
            String b1 = drugService.selectDrugCount(split1);
            if (b1.equals("药品充足!")) {
                boolean b = drugService.updStatus(Integer.parseInt(id), date, split1, split2);
                if (b) {
                    str = "拿药成功!";
                } else {
                    str = "数据走丢了!";
                }
            } else {
                str = b1;
            }
        } else {
            str = "该患者已拿药！";
        }
        return JSON.toJSONString(str);
    }

}
